<template>
  <div class="progress-bar" ref="progressBar" @click="progressClick">
    <div class="bar-inner">
      <div class="progress" ref="progress"></div>
      <div class="progress-btn-wrapper" ref="progressBtn"
        @touchstart.prevent="handleTouchStart"
        @touchmove.prevent="handleTouchMove"
        @touchend="handleTouchEnd"
      >
        <!-- button外包裹一个 wrapper 为了让用户的点击范围变大 -->
        <div class="progress-btn"></div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { prefixStyle } from 'common/js/dom'

const progressBtnWidth = 16
const transform = prefixStyle('transform')

export default {
  props: {
    percent: {
      type: Number,
      default: 0
    }
  },
  created() {
    this.touch = {}
  },
  methods: {
    handleTouchStart(e) {
      this.touch.touchFlag = true
      this.touch.startX = e.touches[0].clientX
      this.touch.left = this.$refs.progress.clientWidth
    },
    handleTouchMove(e) {
      // 计算
      const distanceX = e.touches[0].clientX - this.touch.startX
      const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth
      const offset = Math.min(Math.max(0, this.touch.left + distanceX), barWidth)
      this._offset(offset)
    },
    handleTouchEnd() {
      this._triggerPercent()
      this.touch.touchFlag = false
    },
    progressClick(e) {
      const progressLeft = this.$refs.progressBar.getBoundingClientRect().left
      const eClientX = e.clientX
      this._offset(eClientX - progressLeft)
      this._triggerPercent()
    },
    _triggerPercent() {
      const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth
      const percent = this.$refs.progress.clientWidth / barWidth
      this.$emit('percentChange', percent)
    },
    _offset(offsetWidth) {
      this.$refs.progress.style.width = `${offsetWidth}px`
      this.$refs.progressBtn.style[transform] = `translate3d(${offsetWidth}px,0,0)`
    }
  },
  watch: {
    percent(newPercent) {
      if (newPercent >= 0 && !this.touch.touchFlag) {
        const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth
        const offsetWidth = newPercent * barWidth
        this._offset(offsetWidth)
      }
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import "~common/stylus/variable"

.progress-bar
  height: 30px
  .bar-inner
    position: relative
    top: 13px // 相对自己向下偏移13px
    height: 4px
    background: rgba(0, 0, 0, 0.3)
    .progress
      position: absolute
      height: 100%
      background: $color-theme
      // width: 20px
    .progress-btn-wrapper
      position: absolute
      // 先将 放大区方块的中心点 定位到 progressbar的起点位置
      left: -7px
      top: -13px
      width: 30px
      height: 30px
      .progress-btn
        position: relative
        box-sizing: border-box
        width: 16px
        height: 16px
        background: $color-theme
        border-radius: 50%
        // 再将 正在的butoon的在 放大方块的位置偏移到正确位置
        left: 7px
        top: 7px
        border: 3px solid $color-text
</style>
